﻿package mortal.game.view.skill.panel
{
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.game.cache.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.skill.*;
    import mortal.game.view.skill.panel.render.*;

    public class SkillCareerPanel extends GSprite implements ISkillSubPanel
    {
        private var _items:Array;
        private var _page:PageSelecter;
        private var _desc:SkillLearnDesc;
        private var _lastItem:SkillCareerItem;
        private var _runeBigBg:GImageBitmap;
        private var _bmpCombat:BitmapNumberText;
        private var _runes:SkillRune;
        private var _txtActive:GTextFiled;
        private var _lastIndex:int = -1;
        private var _skillInfos:Array;
        public static const pageNum:int = 5;

        public function SkillCareerPanel()
        {
            return;
        }// end function

        public function refreshRedPoint() : void
        {
            var _loc_2:SkillCareerItem = null;
            if (this._items == null || this._items.length == 0)
            {
                return;
            }
            var _loc_1:int = 0;
            while (_loc_1 < this._items.length)
            {
                
                _loc_2 = this._items[_loc_1] as SkillCareerItem;
                if (_loc_2 != null && _loc_2.skillInfo != null)
                {
                    _loc_2.refreshRedPoint();
                }
                _loc_1++;
            }
            return;
        }// end function

        public function getItemByPos(param1:int) : SkillItem
        {
            var _loc_3:SkillCareerItem = null;
            var _loc_2:int = 0;
            while (_loc_2 < this._items.length)
            {
                
                _loc_3 = this._items[_loc_2] as SkillCareerItem;
                if (_loc_3 != null && _loc_3.skillInfo != null)
                {
                    if (_loc_3.skillInfo.position == param1)
                    {
                        return _loc_3.skillItem;
                    }
                }
                _loc_2++;
            }
            return null;
        }// end function

        public function resetPage() : void
        {
            this._lastIndex = -1;
            return;
        }// end function

        public function selectIndex(param1:int) : void
        {
            var _loc_2:* = param1 / pageNum;
            var _loc_3:* = param1 % pageNum;
            this._page.currentPage = _loc_2 + 1;
            this._lastIndex = param1;
            this.updateToCurPage();
            return;
        }// end function

        public function set allSkillInfos(param1:Array) : void
        {
            this._skillInfos = param1;
            if (this._skillInfos == null)
            {
                return;
            }
            this._page.maxPage = Math.ceil(this._skillInfos.length / pageNum);
            if (this._lastIndex < 0)
            {
                this._page.currentPage = 1;
            }
            this.updateToCurPage();
            return;
        }// end function

        public function updateSkillInfo(param1:SkillInfo) : void
        {
            this._desc.updateData(param1);
            this._runes.updateInfos();
            this._runes.x = (674 - this._runes.width) / 2 + 15;
            this.updateHaveSkillPoint(Cache.instance.login.loginGame.money.skillPoint);
            this._txtActive.htmlText = Language.getStringByParam(21082, Cache.instance.skill.getLearnedCarrerSkills().length, Cache.instance.skill.getSkillCanLearnByPlayerLevel().length);
            this._bmpCombat.text = Cache.instance.pack.runeBag.getRunesTotalCombat().toString();
            return;
        }// end function

        public function updateHaveSkillPoint(param1:int) : void
        {
            if (this._desc != null)
            {
                this._desc.updateHaveSkillPoint(param1);
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_1:int = 0;
            var _loc_2:SkillCareerItem = null;
            super.createDisposedChildrenImpl();
            pushUIToDisposeVec(UIFactory.bg(-2, 0, 688, 474, this));
            this._items = [];
            _loc_1 = 0;
            while (_loc_1 < pageNum)
            {
                
                _loc_2 = UICompomentPool.getUICompoment(SkillCareerItem) as SkillCareerItem;
                this._items.push(_loc_2);
                _loc_2.index = _loc_1;
                _loc_2.y = 10;
                _loc_2.x = _loc_1 * 135 + 5;
                this.addChild(_loc_2);
                _loc_2.selected = false;
                _loc_2.configEventListener(MouseEvent.CLICK, this.clickItemHandler);
                _loc_2.configEventListener(MouseEvent.ROLL_OVER, this.mouseOverItemHandler);
                _loc_2.configEventListener(MouseEvent.ROLL_OUT, this.mouseOutItemHandler);
                _loc_1++;
            }
            this._runeBigBg = UIFactory.gImageBitmap(null, 0, 291, this);
            LoaderHelp.setBitmapdata(ImagesConst.SkillRuneRegionBg + ".swf", this._runeBigBg);
            this._bmpCombat = UIFactory.bitmapNumberText(this._runeBigBg.x + 423, this._runeBigBg.y + 140, "FightInfoNum.png", 12, 15, -1, this);
            this._bmpCombat.align = BitmapNumberText.Mid;
            this._page = UIFactory.pageSelecter(272, 282, this, PageSelecter.CompleteMode);
            this._page.configEventListener(Event.CHANGE, this.pageChangeHandler);
            this._desc = UICompomentPool.getUICompoment(SkillLearnDesc) as SkillLearnDesc;
            this._desc.x = 687;
            this.addChild(this._desc);
            this._runes = UICompomentPool.getUICompoment(SkillRune) as SkillRune;
            this._runes.x = 129;
            this._runes.y = 322;
            this.addChild(this._runes);
            this._txtActive = UIFactory.gTextField("", 430, (this._page.y + 1), 400, 20, this);
            this._txtActive.textColor = 65280;
            LoaderHelp.addResCallBack(ResFileConst.skillPanel, this.skillPanelGot);
            return;
        }// end function

        private function mouseOverItemHandler(event:MouseEvent) : void
        {
            var _loc_2:* = event.target as SkillCareerItem;
            if (_loc_2 != null)
            {
                _loc_2.updateSelectedView(true);
            }
            return;
        }// end function

        private function mouseOutItemHandler(event:MouseEvent) : void
        {
            var _loc_2:* = event.target as SkillCareerItem;
            if (_loc_2 != null)
            {
                _loc_2.selected = _loc_2.selected;
            }
            return;
        }// end function

        private function skillPanelGot() : void
        {
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._page.dispose(param1);
            this._page = null;
            this._desc.dispose(param1);
            this._desc = null;
            DisplayUtil.delNotUse(this._items, 0);
            this._items = null;
            this._lastIndex = -1;
            this._runes.dispose(param1);
            this._runes = null;
            this._runeBigBg.dispose(param1);
            this._runeBigBg = null;
            this._bmpCombat.dispose(param1);
            this._bmpCombat = null;
            this._txtActive.dispose(param1);
            this._txtActive = null;
            return;
        }// end function

        private function pageChangeHandler(event:Event) : void
        {
            this.updateToCurPage();
            return;
        }// end function

        private function clickItemHandler(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as SkillCareerItem;
            this._lastIndex = (this._page.currentPage - 1) * pageNum + _loc_2.index;
            this.setSelected(_loc_2);
            return;
        }// end function

        public function setSelected(param1:SkillCareerItem) : void
        {
            if (this._lastItem != null)
            {
                this._lastItem.selected = false;
            }
            this.updateSkillInfo(param1.skillInfo);
            this._lastItem = param1;
            this._lastItem.selected = true;
            return;
        }// end function

        private function updateToCurPage() : void
        {
            var _loc_5:SkillCareerItem = null;
            var _loc_6:SkillInfo = null;
            var _loc_1:* = this._page.currentPage - 1;
            var _loc_2:* = _loc_1 * pageNum;
            var _loc_3:* = Math.min(_loc_2 + pageNum, this._skillInfos.length);
            var _loc_4:* = _loc_2;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = this._items[_loc_4 - _loc_2];
                _loc_6 = this._skillInfos[_loc_4] as SkillInfo;
                _loc_5.skillInfo = _loc_6;
                _loc_5.visible = true;
                if (this._lastIndex < 0)
                {
                    if (_loc_4 == _loc_2)
                    {
                        this._lastIndex = _loc_4;
                        this.setSelected(_loc_5);
                    }
                }
                else if (this._lastIndex >= _loc_3 || this._lastIndex < _loc_2)
                {
                    if (_loc_4 == _loc_2)
                    {
                        this._lastIndex = _loc_4;
                        this.setSelected(_loc_5);
                    }
                }
                else if (_loc_4 == this._lastIndex)
                {
                    this._lastIndex = _loc_4;
                    this.setSelected(_loc_5);
                }
                _loc_4++;
            }
            _loc_4 = _loc_4 - _loc_2;
            while (_loc_4 < pageNum)
            {
                
                _loc_5 = this._items[_loc_4];
                _loc_5.visible = false;
                _loc_4++;
            }
            return;
        }// end function

    }
}
